package com.gzdh.dishrecognition.ui.adapter.report;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.DiningTableListDto;
import com.gzdh.dishrecognition.R;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class DiningTableReportAdapter extends RecyclerView.Adapter<DiningTableReportAdapter.ViewHolder> {


    private Context mContext;
    private List<DiningTableListDto> reserveListDtoList;

    private UserClick userClick;

    public void setUserClick(UserClick userClick) {
        this.userClick = userClick;
    }

    public void setReserveListDtoList(List<DiningTableListDto> reserveListDtoList) {
        reserveListDtoList.sort(Comparator.comparing(DiningTableListDto::getDiningTime));
        this.reserveListDtoList = reserveListDtoList;
        notifyDataSetChanged();

    }

    public DiningTableReportAdapter(Context context) {
        mContext = context;
        this.reserveListDtoList = new ArrayList<>();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dining_table, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bqTv.setText(reserveListDtoList.get(position).getBqName());
        holder.ksTv.setText(reserveListDtoList.get(position).getKsName());
        holder.totalCostTv.setText(reserveListDtoList.get(position).getTotalCost());
        holder.countTv.setText(reserveListDtoList.get(position).getNumber());
        holder.amountTv.setText(reserveListDtoList.get(position).getPrice());
        holder.timeTv.setText(reserveListDtoList.get(position).getDiningTime());
        holder.useTimesTv.setText(reserveListDtoList.get(position).getUseTimes());
        holder.mainLinear.setOnClickListener(view -> {
            if (userClick != null) {
                userClick.onUserClick(reserveListDtoList.get(position).getKsh(),
                        reserveListDtoList.get(position).getDinner(),
                        reserveListDtoList.get(position).getDate(),
                        reserveListDtoList.get(position).getUseTimes());
            }
        });
    }

    @Override
    public int getItemCount() {
        return reserveListDtoList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;
        TextView bqTv;
        TextView ksTv;
        TextView totalCostTv;
        TextView amountTv;
        TextView timeTv;
        TextView countTv;
        TextView useTimesTv;

        ViewHolder(View item) {
            super(item);
            mainLinear = item.findViewById(R.id.mainLinear);
            bqTv = item.findViewById(R.id.bqTv);
            ksTv = item.findViewById(R.id.ksTv);
            totalCostTv = item.findViewById(R.id.totalCostTv);
            amountTv = item.findViewById(R.id.amountTv);
            timeTv = item.findViewById(R.id.timeTv);
            useTimesTv = item.findViewById(R.id.useTimesTv);
            countTv = item.findViewById(R.id.countTv);
        }
    }

    public interface UserClick {
        void onUserClick(String ksh, String dinner, String dcTime, String times);
    }

}
